package com.manor.paradise.dialog

import android.content.Context
import android.text.InputFilter
import android.text.InputType
import android.text.TextUtils
import android.text.method.PasswordTransformationMethod
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.lxj.xpopup.impl.FullScreenPopupView
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.ClickUtil
import com.manor.common.utils.GsonUtil
import com.manor.common.utils.ToastUtil
import com.manor.paradise.R
import com.manor.paradise.adapter.TimeAdapter
import com.manor.paradise.bean.NobleBean
import com.manor.paradise.bean.ParadiseInfoBean
import com.manor.paradise.bean.QuantitySelectionBean
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import kotlinx.android.synthetic.main.dialog_noble.view.*
import java.math.BigDecimal

/**
 * 兑换贵族
 */
class NoblePopup( context: Context,var data : NobleBean.NobleListBean) : FullScreenPopupView(context), View.OnClickListener {
    internal var arr: MutableList<QuantitySelectionBean> = mutableListOf()
    internal lateinit var numberAdapter: TimeAdapter
    override fun getImplLayoutId(): Int {
        return R.layout.dialog_noble
    }

    override fun onCreate() {
        super.onCreate()
        im_dismiss.setOnClickListener(this)
        tv_name.setText(data.levelname)
        ed_password.setInputType(InputType.TYPE_CLASS_NUMBER) //输入类型
        ed_password.setFilters(arrayOf<InputFilter>(InputFilter.LengthFilter(6))) //最大输入长度
        ed_password.setTransformationMethod(PasswordTransformationMethod.getInstance()) //设置为密码输入框
        ElvesHttpUtil.getParadiseInfo( object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    return
                }

                var paradiseInfoBean: ParadiseInfoBean = GsonUtil.gsonToBean(info, ParadiseInfoBean::class.java)
                tv_ks.setText("我有淘米劵："+ paradiseInfoBean.sdCoupons.toString())

            }

            override fun onError() {
                super.onError()

            }
        })
        Glide.with(this)
                .load(data.thumb)
                .into(im_pic)
        tv_confirm.setOnClickListener {
            if (!TextUtils.isEmpty(ed_password.text.toString()) && ed_password.text.toString().length == 6 && !giftId.equals("")) {
                ElvesHttpUtil.getPlayNoble(giftId,
                        buyDuration,
                        sdCoupons,
                        ed_password.text.toString(),
                        object : JHttpCallback() {
                            override fun onSuccess(code: Int, msg: String, info: String) {
                                ToastUtil.show(msg)
                                if (code != 200) {

                                    return
                                } else {

                                    dismiss()
                                }

                            }

                            override fun onError() {
                                super.onError()

                            }
                        })
            } else {
                if (!giftId.equals("")) {
                    ToastUtil.show("请选择兑换时长")
                }
                if (ed_password.text.toString().length < 6) {
                    ToastUtil.show("请输入6位交易密码")
                }
                if (TextUtils.isEmpty(ed_password.text.toString())) {
                    ToastUtil.show("请输入交易密码")
                }
            }
        }

        rl_sc.setOnClickListener { showpop(rl_sc, data.level_price) }
    }
      var giftId :String = ""
     var buyDuration :Int = 0
    lateinit var sdCoupons :BigDecimal
    private fun showpop(m_view: View, arrayList: MutableList<NobleBean.NobleListBean.LevelPriceBean>) {
        val contentView = LayoutInflater.from(context).inflate(R.layout.dialog_pop_noble, null)
        val mPopWindow = PopupWindow(contentView,
                ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true)
        mPopWindow.setContentView(contentView)
        val mRecyclerView = contentView.findViewById<RecyclerView>(R.id.rv_comments_lisks)
        mRecyclerView.setHasFixedSize(true)
        mRecyclerView.setLayoutManager(LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false))
        numberAdapter = TimeAdapter(R.layout.rv_time_item, arrayList)
        mRecyclerView.setAdapter(numberAdapter)
        if (mPopWindow.isShowing()) {
            mPopWindow.dismiss()
        } else {
            mPopWindow.showAsDropDown(m_view)
        }
        numberAdapter.setOnItemClickListener { adapter, view, position ->
            tv_names.setText(numberAdapter.getItem(position).name.toString())
            tv_sdj.setText(numberAdapter.getItem(position).price.toString())
             giftId  = numberAdapter.getItem(position).id
            sdCoupons  = BigDecimal(numberAdapter.getItem(position).price.toString())
            buyDuration  = numberAdapter.getItem(position).time
            mPopWindow.dismiss()
        }
    }

    override fun onClick(v: View?) {
        if (ClickUtil.canClick(500))
            when (v?.id) {
                R.id.im_dismiss -> {
                    dismiss()
                }
            }
    }
    override fun onDestroy() {
        Glide.get(context).clearMemory()
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_SHOP_BY_UID)
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_NPC_SHOP_BUY)


        super.onDestroy()
    }
}